package com.generalbackend.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.generalbackend.common.model.entity.User;
import com.generalbackend.common.model.query.UpdatePasswordQuery;
import com.generalbackend.common.model.vo.UserVO;
import com.generalbackend.common.service.IEtoolsUserService;
import com.generalbackend.common.service.IFileService;
import com.generalbackend.common.service.IPersonalInformationService;
import com.generalbackend.common.util.ClientUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author tim.xie
 * @date 2022/7/8 16:20
 */
@Service
public class PersonalInformationServiceImpl implements IPersonalInformationService {

    @Autowired
    private IEtoolsUserService userService;
    @Autowired
    private IFileService fileService;
    @Autowired
    private PasswordEncoder pw;
    private static final String USER_AVATAR_CHILD_DIR = "userAvatar";

    @Override
    public UserVO getPersonalInformation() {
        UserVO userVO = userService.getUserVoById(ClientUtil.getUser().getId());
        return userVO;
    }

    @Override
    public boolean updateUser(UserVO userVO) {
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<User>()
                .set(StringUtils.isNotBlank(userVO.getRealName()), User::getRealName, userVO.getRealName())
                .set(userVO.getAvatar() != null, User::getAvatar, userVO.getAvatar())
                .eq(User::getId, userVO.getId());
        return userService.update(updateWrapper);
    }

    @Override
    public String uploadAvatar(MultipartFile file) {
        String path = null;
        try {
            path = fileService.uploadFile(file, USER_AVATAR_CHILD_DIR);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return path;
    }

    @Override
    public void downloadAvatar(HttpServletResponse response, String childDir, String randomFolder, String filename) {
        fileService.downloadFile(response, childDir, randomFolder, filename);
    }

    @Override
    public boolean updatePassword(UpdatePasswordQuery updatePasswordQuery) {
        User user = userService.getById(ClientUtil.getUser().getId());
        if (!pw.matches(updatePasswordQuery.getOldPassword(), user.getPassword())) {
            return false;
        }
        String newEncodePassowd = pw.encode(updatePasswordQuery.getNewPassword());
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<User>()
                .set(User::getPassword, newEncodePassowd)
                .eq(User::getId, user.getId());
        boolean update = userService.update(updateWrapper);
        if (update == true) {
            ClientUtil.getSession().invalidate();
        }
        return update;
    }

}
